package org.blame.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 何彦声
 * Date: 2025-08-27
 * Time:
 */

@RequestMapping("/chat")
@RestController
public class ChatClientController {

    @Autowired
    private ChatClient chatClient;
//    private final ChatClient chatClient;
//
//    public ChatClientController(ChatClient.Builder chatClientBuilder) {
//        this.chatClient = chatClientBuilder.defaultSystem("你的名字叫小瑞，你是一个全能助手").build();
//    }

    @GetMapping("/call")
    public String generation(String userInput) {
        return this.chatClient.prompt()
                .user(userInput)
                //请求⼤模型
                .advisors(new SimpleLoggerAdvisor())//设置单个对话的advisor
                .call()
                //返回⽂本
                .content();
    }

    record Recipe(String dishName, List<String> ingredients){}

    @GetMapping("/entity")
    public String entity(String userPut){
        Recipe recipe=this.chatClient.prompt()
                .user(String.format("请帮我生成%s的食谱",userPut))
                .call()
                .entity(Recipe.class);
        return recipe.toString();
    }

    @GetMapping(value = "/stream", produces = "text/html;charset=utf-8")
    public Flux<String> stream(String userPut){
        return this.chatClient.prompt()
                .user(userPut)
                .stream()
                .content();
    }
}
